//
//  DeviceListViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/27.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "DeviceListViewController.h"
#import "DeviceListCell.h"
#import "CompleteUserInfoViewController.h"
#import "ScanBindDeviceViewController.h"
#import "UDPManage.h"
@interface DeviceListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,strong)NSMutableArray *deviceList;
@end

@implementation DeviceListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"设备列表";
    [self configTableView];
    [[UDPManage shareUDPManage] broadcast];
    kWeakSelf;
    [UDPManage shareUDPManage].macCallBackBlock = ^(NSString * _Nonnull macAddress) {
        kStrongSelf;
        [strongSelf.deviceList addObject:@{@"mac":macAddress}];
        dispatch_async(dispatch_get_main_queue(), ^{
            [strongSelf.mainTableView reloadData];
        });
    };
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"DeviceListCell" bundle:nil] forCellReuseIdentifier:@"DeviceListCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.deviceList.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DeviceListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeviceListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    if (indexPath.row==0) {
        [cell.bgView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
    }else if (indexPath.row<2-1){
        [cell.bgView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
    }else{
        [cell.bgView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
    }
    NSDictionary *dic = [self.deviceList objectAtIndex:indexPath.row];
    cell.stateLab.hidden = YES;
    cell.titleLab.text = @"新设备";
    cell.snCodeLab.text = [NSString stringWithFormat:@"设备号：%@",[dic objectForKey:@"mac"]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.deviceList objectAtIndex:indexPath.row];
    ScanBindDeviceViewController *scanBindDevice = [[ScanBindDeviceViewController alloc]init];
    scanBindDevice.snCode = [NSString stringWithFormat:@"MAC:%@",[dic objectForKey:@"mac"]];
    [self.rt_navigationController pushViewController:scanBindDevice animated:YES complete:nil];
}

#pragma mark - instance
-(NSMutableArray*)deviceList{
    if (!_deviceList) {
        _deviceList = [[NSMutableArray alloc]init];
    }
    return _deviceList;
}
@end
